/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import com.puny.utils.DisplacementMapGenerator;
	import com.puny.utils.easing.IEasing;
	import com.puny.utils.easing.OneEndEasing;
	
	import flash.geom.Point;

	public class WaveDisplacementMap extends DisplacementMapGenerator
	{
		private var _peeks:int;
		private var _amp:Number;
		private var _easing:IEasing;
		private var _weight:Number = 1;
		private var _lastYdd:Number;
		public function WaveDisplacementMap(width:Number, height:Number, peeks:int=3, amp:Number=6)
		{
			_peeks = peeks;
			_amp = amp;
			_easing = new OneEndEasing(1,0,(height/2),3);
			super(width, height);
		}
		
		override protected function calcurate(xdd:Number, ydd:Number):Point {
//			trace(xdd, ydd,_weight);
			var l:Number = Math.sqrt(xdd * xdd + ydd * ydd);
			if (l == 0) {
				return new Point(0,0);
			}
			if (ydd > 0 && _lastYdd != ydd) {
				_weight = _easing.forward();
				_lastYdd = ydd;
			} else if (ydd < 0) {
				_easing.initialize();
				_weight = 1;
			}
			var dd:Number = _amp*Math.sin(l/(width/_peeks)*2*Math.PI) * _weight;
			return new Point(xdd+dd/l*xdd, ydd+dd/l*ydd);
		}
	}
}